<?php
namespace Common\Model;

class ClickDetailModel extends Model {
    protected $tableName       = 'template.click_detail';
    private   $tableName2      = 'click_detail';
    protected $pk              = 'id';

    const TABLE_PARTITION_NUM = 20;  //表格分割数量

    /**
     * _parseOptions
     * 拦截_parseOptions主要是为了自动生成表名
     * 生成表名时必须用到的参数有day和uid,也就是where()的条件必须要有day和uid
     *
     * @param mixed $options
     * @return void
     */
    protected function _parseOptions($options = null) {
        if(empty($this->trueTableName) || ($this->trueTableName == $this->tableName)) {
            $day = empty($this->options['where']['day']) ? null : $this->options['where']['day'];
            $uid = empty($this->options['where']['uid']) ? null : $this->options['where']['uid'];

            if(empty($day) || empty($uid)) {
                throw new ThinkException('请确认是否有day或uid');
            }

            $timestamp  = strtotime($day);
            $tableIndex = $uid % self::TABLE_PARTITION_NUM;
            $tableName  = 'd' . date('Ymd', $timestamp) . '.'. $this->tableName2 . '_' . $tableIndex;
            $options    = array_merge($options, array('table' => $tableName));

            $this->trueTableName = $tableName;

            unset($options['where']['day']);    //表中无day字段，用day主要是定位到对应日期的表上
        }

        return parent::_parseOptions($options);
    }

    /**
     * buildSearchExpr 
     * 组装查询条件
     * 
     * @param mixed $d  查询数据，一般来源于$_GET或$_POST
     * @param mixed $field  可查询的字段对照表，其结构为array(array('表单项目名称' => '数据库字段名称'))
     * @access public
     * @return void 返回值为数组，参见方法内部注释
     */
    public function buildSearchExpr($d, $field = NULL) {
        $expr = array();
        $dao  = array();
        $sortExpr = 'id DESC';

        /**
         * 下标为表单中的项目名称，值为数据库中表的字段名称 
         */
        if(empty($field)) {
            $field = array(
                'day'     => 'day',
                'uid'     => 'uid',
                'plan_id' => 'plan_id',
                'zone_id' => 'zone_id',
                'site_id' => 'site_id',
                'ad_id'   => 'ad_id',
                'ad_type' => 'ad_type',
                'ip'      => 'ip',
            );
        }

        foreach($field as $k => $v) {
            $tmpExpr = null;
            $val     = isset($d[$k]) ? trim($d[$k]) : '';

            if(strlen($val) < 1) {
                continue;
            }

            $expr[$v] = $d[$k];
            $dao[$k]  = $d[$k];
        }

        if(!empty($d['sort']) && in_array($d['sort'], $this->getDbFields())) {
            $sortdir = empty($d['sortdir'])                     ? 'DESC'   : strtoupper($d['sortdir']);
            $sortdir = in_array($sortdir, array('DESC', 'ASC')) ? $sortdir : 'DESC';

            $sort           = $d['sort'];
            $dao['sort']    = $sort;
            $sortExpr       = "{$sort} {$sortdir}";
            $dao['sortdir'] = $sortdir;
        }

        return array(
            'expr' => $expr,            //ThinkPHP查询条件表达式
            'sort' => $sortExpr,        //排序表达式
            'json' => json_encode($dao) //用于回填至查询表单的数据
        );
    }

}
